<div id="main" class="">
    <div id="page-body" class="calculator">
        <!-- Commission Calculator -->
        <div id="commission">
            <div class="heading">
                <h1>How much will<br> you save?</h1>
                <strong><span class="white">Commission savings calculator</span></strong>
            </div>
            <script type="text/javascript">// <![CDATA[
                $(document).ready(function() {
                    $('select[name="calculator-state"]').change(getstate);
                    $('select[name="calculator-sales"]').change(getstate);
                });

                //The function that handles the process
                function getstate(event)
                {
                    //Stop the form from submitting
                    event.preventDefault();

                    //Collect our form data.
                    var form_data = {
                        calculator_state: $('select[name="calculator-state"]').val(),
                        calculator_sales: $('select[name="calculator-sales"]').val(),
                    };

                    //Begin the ajax call
                    $.ajax({
                        url: "<?php echo site_url('home/calculator_ajax') ?>",
                        type: "POST",
                        data: form_data,
                        dataType: "json",
                        cache: false,
                        success: function(json) {
                            if (json.error == 1)
                            {

                            } else {
                                //Hide our form
                                // $('#registerModalform').slideUp();
                                //Show the success message
                                $('#save_values_default').hide();
                                $('#state_message_default').hide();
                                $('#save_values').html(json.save_values).show();
                                $('#state_message').html(json.state_message).show();

                            }
                        }
                    });
                }

                // ]]>
            </script>
            <div class="calculator">
                <?php echo form_open(site_url('home/index')); ?>
                <?php echo $state; ?>    
                <br /> 
                <?php echo $sales; ?>
                <?php echo form_close(); ?> 


                <div class="savings clearfix">
                    <h2>You Save</h2>
                    <div class="value clearfix">
                        <span class="dollars"></span> 
                        <span data-input-type="label" class="result">
                            <div id="save_values_default" >$ 840</div>
                            <div id="save_values" ></div>
                        </span>
                        <div class="clearer"></div>
                    </div>
                    <div class="clearer"></div>
                </div>
                <div class="disclaimer">
                    <p>
                    <div id="state_message_default" >Our savings are based on Qld Industry Standard Maximum Commission charged at 5% of the first $18,000 + 2.5% of the balance of the settled price + 10% GST. For a Fair Go - Go Gecko.</div>
                    <div id="state_message" ></div>
                    </p>
                </div>
            </div>
            <div class="clearfix"></div>
        </div>
    </div>
    <script type="text/javascript">
        (function($) {
            /*
             * -----------------------------
             * Commission savings calculator
             * -----------------------------
             */

            // Initialize price dropdown
            var state_dropdown = $("#commission .state-dropdown");
            var sale_price_selector = $("#commission .sale-price");
            var prices = new Array();
            var start = 2;
            var end = 8;
            var step = 0.5;

            for (var i = start; i <= end; i += step) {
                var value = i * 100000;
                sale_price_selector.append($("<option></option>")
                        .attr("value", value)
                        .text("$" + formatCurrency(value)));
            }

            // Select value on click
            $("#commission .state-dropdown").change(function(e) {

                // Set select value 
                var selected = $(this).find("option:selected").attr("value");
                state_dropdown.attr("value", selected);

                // Toggle disclaimer
                $("#commission .disclaimer span").removeClass("active");
                if (selected == "QLD") {
                    $("#commission .disclaimer span.queensland").addClass("active");
                } else {
                    $("#commission .disclaimer span.other").addClass("active");
                }
                // Do calculations
                calculateSavings();
            });
            $("#commission .sale-price").change(function(e) {

                // Set select value 
                var selected = $(this).find("option:selected").attr("value");
                sale_price_selector.attr("value", selected);

                // Do calculations
                calculateSavings();
            });

            // Calculate and display savings
            var go_gecko_commission = 6950;
            var go_gecko_commission_at_250k = 5950;
            var go_gecko_commission_at_200k = 4950;
            var savings_label = $("#commission .calculator [data-input-type='label']");
            function calculateSavings() {

                // Get selected state, cancel if none selected
                var state = state_dropdown.attr("value");
                if (!state) {
                    return;
                }

                // Get sale price
                var sale_price = sale_price_selector.attr("value");

                if (sale_price >= 500000) {
                    savings_label.text("Thousands");
                    $("#commission .calculator .savings .value .dollars").text("");
                } else {

                    // Calculate savings
                    var standard_commission;
                    if (state === "QLD") {
                        standard_commission = Math.floor(((18000 * 0.05) + ((sale_price - 18000) * 0.025)) * 1.1);
                    }
                    else {
                        standard_commission = Math.floor(sale_price * 0.025 * 1.1);
                    }

                    var savings = standard_commission;

                    switch (parseInt(sale_price)) {
                        case 200000:
                            savings -= go_gecko_commission_at_200k;
                            break;
                        case 250000:
                            savings -= go_gecko_commission_at_250k;
                            break;
                        default:
                            savings -= go_gecko_commission;
                            break;
                    }

                    // Display savings
                    savings_label.text(formatCurrency(savings));
                    $("#commission .calculator .savings .value .dollars").text("$");
                }

            }

            // Format price
            function formatCurrency(value) {
                value = value.toString();
                var d = 0;
                for (var i = value.length; i > 0; i--) {
                    if (d++ % 3 === 0 && d > 1) {
                        value = value.substring(0, i) + "," + value.substring(i);
                    }
                }
                return value;
            }


            // Initial state is QLD
            calculateSavings();


        })(jQuery);
    </script>
</div><!-- #main -->